/************************************
 * Package: RGcpp					*
 * Authors: Rui Eduardo Gouveia Gil	*
 * Date: 2010						*
 ************************************/
#ifndef __RGCPP_UNIXSOCKET_H__
#define __RGCPP_UNIXSOCKET_H__

#ifndef _WIN32
#include <string>
#include <errno.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <boost/thread/mutex.hpp>

#include "rgcpp/exceptions/IOException.h"

namespace rgcpp
{

#define NO_TIMEOUT 0

class UnixSocket
{
	friend class UnixServerSocket;

	protected:
		int socketFD;
		bool releaseForced;
		boost::mutex mutRead;
		boost::mutex mutWrite;
		struct timeval timeout;

	public:
		UnixSocket(const std::string& socketPath);
		~UnixSocket();

		int readData(char* buffer, const unsigned int size);
		void writeData(const char* buffer, const unsigned int size);

		void setTimeout(const unsigned long timeout);
		unsigned long getTimeout();

		void release();

		bool check4readData(const unsigned long timeout);

	protected:
		UnixSocket(int socketFD);
};

}
#endif

#endif
